<template>
  <div class="phone-mockup">
    <div class="phone-header">
      <img src="@/assets/assets/phone-head.png" alt="" />
      <h3 class="text-center border-b-1 border-gray-300">战略微头条</h3>
    </div>
    <div class="content">
      <h2 class="title">战略微头条 | (NO.07) 2024.08.05-08.11</h2>
      <div class="meta">
        <span class="read-count">👁️ 0人阅读</span>
        <span class="time">🕒 --</span>
      </div>

      <div class="news-title">
        <span class="diamond">◆</span>
        <span class="main-title">新闻周报</span>
        <span class="diamond">◆</span>
      </div>

      <div v-if="pendingNews.length > 0" class="news-items-container">
        <div v-for="(news, index) in pendingNews" :key="news.id || index" class="news-item">
          <div class="news-number flex half-image-container">
            <img src="@/assets/assets/0.svg" class="half-image top-half" />
            <img src="@/assets/assets/1.svg" class="half-image top-half" />
          </div>
          <h3>{{ news.title }}</h3>
          <p class="source">{{ news.author }}</p>
          <p class="summary">{{ news.summary }}</p>
          <a href="#" class="read-more">阅读全文</a>
          <div class="divider"></div>
        </div>
      </div>

      <div v-else class="no-news">
        <p>暂无新闻数据</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { publishNews } from '@/API/AllApi'
import { ElMessage } from 'element-plus'

const exitPreview = () => {
  alert('退出预览')
}

const pendingNews = ref([])
const loading = ref(false)
const error = ref('')

const fetchNewsList = async () => {
  loading.value = true
  error.value = ''
  try {
    const result = await publishNews()
    const dataArray = Array.isArray(result.data) ? result.data : result.data?.list || []

    pendingNews.value = dataArray.map((item) => ({
      id: item.id || item.newsId || item.news_id || '',
      source: item.siteName || '',
      updater: item.opPerson || '',
      classReal: item.classReal || '',
      publishTime: item.newsTime || '',
      category: item.classModel || '',
      title: item.newsTitle || '',
      summary: item.abstractReal || '',
      author: item.newsSource || '',
      authorIcon: item.newsSource?.charAt(0).toUpperCase() || '?',
      originalData: item,
    }))

    if (pendingNews.value.length === 0) {
      ElMessage.info('暂无待发布新闻')
    }
  } catch (err) {
    console.error('获取待发布新闻列表失败:', err)
    error.value = '获取待发布新闻失败，请稍后重试'
    ElMessage.error(error.value)
  } finally {
    loading.value = false
  }
}

onMounted(async () => {
  await fetchNewsList()
})
</script>

<style scoped>
.phone-mockup {
  width: 375px;
  height: 700px;
  border-radius: 40px;
  border: 8px solid black;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  position: relative;
  background-color: white;
}

.phone-header {
  height: 80px;
  overflow: hidden;
}

.content {
  padding: 20px;
  height: calc(100% - 80px);
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-width: none;
}

.content::-webkit-scrollbar {
  display: none;
}

.title {
  font-size: 18px;
  margin-bottom: 15px;
  font-weight: bold;
}

.meta {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  font-size: 14px;
  color: #666;
}

.read-count,
.time {
  display: flex;
  align-items: center;
  gap: 5px;
}

.news-title {
  text-align: center;
  margin: 20px 0;
  font-size: 22px;
  color: #1a73e8;
  font-weight: bold;
  position: relative;
}

.news-title .diamond {
  color: #1a73e8;
  font-size: 20px;
  margin: 0 10px;
}

.news-title .main-title {
  position: relative;
  padding-bottom: 5px;
  border-top: 2px solid #1a73e8;
  border-bottom: 2px solid #1a73e8;
}

.news-item {
  margin-bottom: 20px;
  padding: 15px;
}

.news-number {
  color: #1a73e8;
  font-size: 24px;
  margin-right: 10px;
  vertical-align: middle;
  display: flex;
  align-items: center;
}

.half-image-container {
  display: flex;
  align-items: flex-start;
  height: 20px;
  position: relative;
}

.half-image {
  width: 30px;
  height: 30px;
  object-fit: contain;
  transform: translateY(20%);
  clip-path: polygon(0 0, 100% 0, 100% 55%, 0 70%);
  margin-right: 2px;
}

.news-item h3 {
  margin: 5px 0 10px;
  font-size: 16px;
  color: #333;
}

.source {
  color: #666;
  font-size: 14px;
  margin: 10px 0;
}

.summary {
  color: #555;
  line-height: 1.5;
  margin-bottom: 15px;
}

.read-more {
  color: #1a73e8;
  text-decoration: none;
  font-size: 14px;
  display: inline-block;
  margin-bottom: 10px;
}

.divider {
  width: 90%;
  height: 2px;
  background-color: #1890ff;
  margin-top: 10px;
  position: relative;
}

.divider::after {
  content: '///';
  position: absolute;
  right: -15%;
  top: -10px;
  color: #1a73e8;
  font-size: 14px;
  font-weight: bold;
}

.no-news {
  text-align: center;
  padding: 20px;
  color: #999;
  font-size: 14px;
}
</style>
